﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Base

Public Class PhieuKhamBenh
    Dim OldValue, NewValue As Decimal
    Dim m_Flag As Boolean
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.GetTonKho_Lo_HanDung_ThucTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTonKho_Lo_HanDung_Thuc, CDate(Today()), CDate(Today()), " ")
        Me.DMDonViTinhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMDonViTinh)
        Me.DMCachDungTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMCachDung)
        Me.BHBangGiaDMHangTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBangGiaDMHang)
        Me.ToaThuocMauTableAdapter.Fill(Me.PCV_CLINICDataSet.ToaThuocMau)
        Me.DMDichVuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMDichVu)
        Me.PhieuKhamBenhChuyenVienTableAdapter.Fill(Me.PCV_CLINICDataSet.PhieuKhamBenhChuyenVien)
        Me.PhieuKhamBenhChuyenKhoaTableAdapter.Fill(Me.PCV_CLINICDataSet.PhieuKhamBenhChuyenKhoa)
        Me.TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TiepNhanBenhNhan)
        Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenhNhan)
        Me.DMTinhTrangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMTinhTrang)
        Me.GioiTinhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.GioiTinh)
        Me.DMDanDoTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMDanDo)
        Me.DMIcdTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMIcd)
        Me.DMBacSiTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBacSi)
        Me.DoSinhHieuTableAdapter.Fill(Me.PCV_CLINICDataSet.DoSinhHieu)
        Me.PhieuKhamBenhToaThuocTableAdapter.Fill(Me.PCV_CLINICDataSet.PhieuKhamBenhToaThuoc)
        Me.PhieuKhamBenhDichVuTableAdapter.Fill(Me.PCV_CLINICDataSet.PhieuKhamBenhDichVu)
        Me.PhieuKhamBenhTableAdapter.Fill(Me.PCV_CLINICDataSet.PhieuKhamBenh)
        Me.XuLyTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.XuLy)
        Me.DMPhongTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMPhong)
        Me.IGridControlDs01.ForceInitialize()
        Me.IGridViewDs01.MoveLast()
        Me.setHeaderFooter("Công Ty CP Tư Vấn Giải Pháp ERP Phong Cách Việt", "Lâm Văn Bền, P.Tân Kiểng, Q.7, TP.HCM")
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub PhieuKhamBenh_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        loadDuLieu()

    End Sub

    Public Sub New()
        Me.IGridViewPopUp1.setColumnCheck()
        Me.IGridViewPopUp2.setColumnCheck()
        InitializeComponent()
        Me.setSoTabThamChieu(2)
        Me.setSoTab(4)
        Me.setPopUp1(Me.PCV_CLINICDataSet_Don.DMDichVu, Me.DMDichVuBindingSource, Me.DMDichVuTableAdapter)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.PhieuKhamBenh, Me.PhieuKhamBenhBindingSource, Me.TableAdapterManager, Me.PhieuKhamBenhTableAdapter, Me.SophieuITextEdit, Me.GhichuITextEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.PhieuKhamBenhDichVu, Me.FKPhieuKhamBenhDichVuPhieuKhamBenhBindingSource, Me.PhieuKhamBenhDichVuTableAdapter)
        Me.setTab2(Me.PCV_CLINICDataSet.PhieuKhamBenhToaThuoc, Me.FKPhieuKhamBenhToaThuocPhieuKhamBenhToaThuocBindingSource, Me.PhieuKhamBenhToaThuocTableAdapter)
        Me.setTab3(Me.PCV_CLINICDataSet.PhieuKhamBenhChuyenKhoa, Me.FKPhieuKhamBenma15702A09BindingSource, Me.PhieuKhamBenhChuyenKhoaTableAdapter)
        Me.setTab4(Me.PCV_CLINICDataSet.PhieuKhamBenhChuyenVien, Me.FKPhieuKhamBenhChuyenVienPhieuKhamBenhBindingSource, Me.PhieuKhamBenhChuyenVienTableAdapter)
        Me.setColumnsChangeTab1(New ArrayList From {"soluong", "dongia"})
        Me.setColumnValueTab1(colEx1ChiTiet01, colthanhtien2)
        Me.setColumnsChangeTab2(New ArrayList From {"soluong", "dongia"})
        Me.setColumnValueTab2(colEx1ChiTiet02, colthanhtien1)
        Me.IGridViewChiTiet01.setBatLoi("soluong", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("dongia", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("thanhtien", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("soluong", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("dongia", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("thanhtien", "not 0 and not null")
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.NgaykhamIDateEdit.EditValue = Today
        Me.setMacDinhCotAn("ma", "PKB" & System.Guid.NewGuid().ToString)
    End Sub

    Private Sub IGridViewChiTiet01_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("sophieu", Me.SophieuITextEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mabenhnhan", Me.MabenhnhanISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongia", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluong", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("thanhtien", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("baohiemchi", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab1(True)
    End Sub

    Private Sub IGridViewChiTiet02_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet02.InitNewRow
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("sophieu", Me.SophieuITextEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("mabenhnhan", Me.MabenhnhanISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("dongia", 0)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("soluong", 0)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("thanhtien", 0)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab2(True)

    End Sub

    Private Sub IGridViewDsThamChieu1_DoubleClick(sender As System.Object, e As System.EventArgs) Handles IGridViewDsThamChieu1.DoubleClick
        If getState() <> 0 Then
            Me.MabenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mabenhnhan")
            Me.TenbenhnhanITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ten")
            Me.TuoiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("tuoi")
            Me.GioitinhISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("gioitinh")
            Me.DiachiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("diachi")
            Me.MachITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mach")
            Me.SobhytITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("sobhyt")
            Me.HanbhytIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("hanbhyt")
            Me.HuyetapITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("huyetap")
            Me.NhietdoITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("nhietdo")
            Me.NhipthoITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("nhiptho")
        End If
    End Sub

    Private Sub TaikhamsauITextEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles TaikhamsauITextEdit.EditValueChanged
        Try
            If getState() <> 0 AndAlso Not IsDBNull(Me.TaikhamsauITextEdit.EditValue) AndAlso Not IsNothing(Me.TaikhamsauITextEdit.EditValue) AndAlso Me.TaikhamsauITextEdit.EditValue.ToString <> "" Then
                Me.NgaytaikhamIDateEdit.EditValue = DateAdd(DateInterval.Day, CDbl(Me.TaikhamsauITextEdit.EditValue), Me.NgaykhamIDateEdit.EditValue)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    'Private Sub RadioGroup1_EditValueChanged(sender As Object, e As System.EventArgs) Handles RadioGroup1.EditValueChanged
    '    If Me.RadioGroup1.SelectedIndex = 0 Then
    '        Me.TiepNhanBenhNhanTableAdapter.FillTiepNhanBenhNhan_Ngay(Me.PCV_CLINICDataSet_Don.TiepNhanBenhNhan, CDate(Today.ToString))
    '    Else
    '        Me.TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TiepNhanBenhNhan)
    '    End If
    'End Sub

    Private Sub IGridViewDsThamChieu2_DoubleClick(sender As System.Object, e As System.EventArgs) Handles IGridViewDsThamChieu2.DoubleClick
        If getState() <> 0 Then
            Me.MabenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("mabenhnhan")
            Me.TenbenhnhanITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("ten")
            Me.TuoiITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("tuoi")
            Me.GioitinhISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("gioitinh")
            Me.DiachiITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("diachi")
            Me.SobhytITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("sobhyt")
            Me.HanbhytIDateEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("ngayhethan")
            'Me.MachITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("mach")
            'Me.HuyetapITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("huyetap")
            'Me.NhietdoITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("nhietdo")
            'Me.NhipthoITextEdit.EditValue = Me.IGridViewDsThamChieu2.GetFocusedRowCellValue("nhiptho")
        End If
    End Sub

    Private Sub RepositoryItemISearchLookUpEdit_Phong_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_Phong.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet03.SetFocusedRowCellValue("maphong", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewChiTiet03.SetFocusedRowCellValue("tenphong", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
    End Sub

    Private Sub IGridViewChiTiet03_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet03.InitNewRow
        Me.IGridViewChiTiet03.SetFocusedRowCellValue("sophieu", Me.SophieuITextEdit.EditValue)
        'Me.IGridViewChiTiet03.SetFocusedRowCellValue("mabenhnhan", Me.MabenhnhanISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet03.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab3(True)
    End Sub

    Private Sub IGridViewChiTiet04_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("sophieu", Me.SophieuITextEdit.EditValue)
        'Me.IGridViewChiTiet03.SetFocusedRowCellValue("mabenhnhan", Me.MabenhnhanISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab4(True)
    End Sub

    Private Sub XulyISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles XulyISearchLookupEdit.EditValueChanged
        If Not IsDBNull(Me.XulyISearchLookupEdit.EditValue) And Not IsNothing(Me.XulyISearchLookupEdit.EditValue) Then
            If Me.XulyISearchLookupEdit.EditValue.ToString = "KET" Then
                Me.TabbedControlGroupChiTiet.SelectedTabPageIndex = 1
                Me.PopupContainerEdit1.Enabled = True
            ElseIf Me.XulyISearchLookupEdit.EditValue.ToString = "CHK" Then
                Me.TabbedControlGroupChiTiet.SelectedTabPageIndex = 2
            ElseIf Me.XulyISearchLookupEdit.EditValue.ToString = "CHVGT" Then
                Me.TabbedControlGroupChiTiet.SelectedTabPageIndex = 3
                Me.PopupContainerEdit2.Enabled = True
            ElseIf Me.XulyISearchLookupEdit.EditValue.ToString = "CHDDV" Then
                Me.TabbedControlGroupChiTiet.SelectedTabPageIndex = 0
            End If
        End If
    End Sub

    Private Sub KhámBệnhToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles KhámBệnhToolStripMenuItem.Click
        If getState() <> 0 Then
            Me.MabenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mabenhnhan")
            Me.TenbenhnhanITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ten")
            Me.TuoiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("tuoi")
            Me.GioitinhISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("gioitinh")
            Me.DiachiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("diachi")
            Me.MachITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mach")
            Me.SobhytITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("sobhyt")
            Me.HanbhytIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("hanbhyt")
            Me.HuyetapITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("huyetap")
            Me.NhietdoITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("nhietdo")
            Me.NhipthoITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("nhiptho")
        End If
    End Sub

    Private Sub BarSubItemPhieuChiDinh_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItemPhieuChiDinh.ItemClick
        Dim report As Report_PhieuChiDinh = New Report_PhieuChiDinh
        report.PhieuKhamBenhDichVuTableAdapter1.FillMa_PhieuChiDinh_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenhDichVu, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.PhieuKhamBenhTableAdapter1.FillMa_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenh, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.ChanDoan.Value = Me.ChandoanISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.ChandoanISearchLookupEdit.EditValue)
        report.NgayThangPhieuChiDinh.Value = "Ngày " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub

    Private Sub BarSubItemPhieuChuyenKhoa_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItemPhieuChuyenKhoa.ItemClick
        Dim report As Report_PhieuChuyenKhoa = New Report_PhieuChuyenKhoa
        report.PhieuKhamBenhTableAdapter.FillMa_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenh, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.PhieuKhamBenhChuyenKhoaTableAdapter1.FillMa_PhieuChuyenKhoa_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenhChuyenKhoa, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.NgayThangPhieuChuyenKHoa.Value = "Ngày " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub

    Private Sub BarSubItemPhieuChuyenVien_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItemPhieuChuyenVien.ItemClick
        Dim report As Report_PhieuChuyenVien = New Report_PhieuChuyenVien
        report.PhieuKhamBenhTableAdapter.FillMa_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenh, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.PhieuKhamBenhChuyenVienTableAdapter1.FillMa_PhieuChuyenVien_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenhChuyenVien, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.NgayThangPhieuChuyenVien.Value = "Ngày " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub

    Private Sub BarSubItemToaThuoc_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItemToaThuoc.ItemClick
        Dim report As Report_ToaThuoc = New Report_ToaThuoc
        report.PhieuKhamBenhTableAdapter.FillMa_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenh, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.PhieuKhamBenhToaThuocTableAdapter1.FillMa_ToaThuoc_PhieuKhamBenh(report.PcV_CLINICDataSet1.PhieuKhamBenhToaThuoc, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.ChanDoan.Value = Me.ChandoanISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.ChandoanISearchLookupEdit.EditValue)
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaykhamIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub

    Private Sub BarButtonItemLuuPopUp1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuuPopUp1.ItemClick
        For index = 0 To Me.IGridViewPopUp1.RowCount - 1
            If CBool(Me.IGridViewPopUp1.GetRowCellValue(index, "Mark")) = True AndAlso Me.getKiemTraMaTonTaiIGridViewChitiet01(Me.IGridViewPopUp1.GetRowCellValue(index, "ma")) Then

                Me.IGridViewChiTiet01.SetFocusedRowCellValue("madichvu", Me.IGridViewPopUp1.GetRowCellValue(index, "ma").ToString)
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("tendichvu", Me.IGridViewPopUp1.GetRowCellValue(index, "ten").ToString)
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongia", CDec(Me.IGridViewPopUp1.GetRowCellValue(index, "dongia")))
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("noithuchien", Me.IGridViewPopUp1.GetRowCellValue(index, "noithuchien").ToString)
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluong", 1)
                Me.IGridViewChiTiet01.AddNewRow()
            End If
            Me.IGridViewPopUp1.SetRowCellValue(index, "Mark", False)
        Next
        'delete row cuối cùng bị dư
        If IsDBNull(Me.IGridViewChiTiet01.GetFocusedRowCellValue("madichvu")) Then
            Me.IGridViewChiTiet01.DeleteSelectedRows()
        End If
        'Duyệt xem trong grid có dòng dư
        For index = 0 To IGridViewChiTiet01.RowCount
            If IsDBNull(Me.IGridViewChiTiet01.GetRowCellValue(index, "madichvu")) Then
                Me.IGridViewChiTiet01.DeleteRow(index)
            End If
        Next
    End Sub
    'Kiểm tra bệnh nhân đã tồn tại trong IGridViewChitiet01
    Private Function getKiemTraMaTonTaiIGridViewChitiet01(ByVal sMadichvu As String) As Boolean 'kiểm tra sự tồn tại của mã - dùng kiểm tra chung với igridview
        Dim acc As Integer
        For index = 0 To Me.IGridViewChiTiet01.RowCount - 2
            If Me.IGridViewChiTiet01.GetRowCellValue(index, "madichvu").ToString.ToUpper = sMadichvu.ToUpper Then
                acc = acc + 1
            End If
        Next
        If acc > 0 Then
            Return False
        End If
        Return True
    End Function

    Private Sub BarButtonItemLuuPopUp2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuuPopUp2.ItemClick
        For index = 0 To Me.IGridViewPopUp2.RowCount - 1
            If CBool(Me.IGridViewPopUp2.GetRowCellValue(index, "Mark")) = True Then
                Me.IGridViewChiTiet02.AddNewRow()
                Dim dt As DataTable = Me.Fill_ToaThuocMauChiTietTableAdapter.GetData(Me.IGridViewPopUp2.GetRowCellValue(index, "ma"))
                For Each row As DataRow In dt.Rows
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("mathuoc", row.Item("mahang").ToString)
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("tenthuoc", row.Item("tenhang").ToString)
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("dvt", row.Item("dvt").ToString)
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("soluong", row.Item("soluong"))
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("cachdung", row.Item("cachdung").ToString)
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("landung", row.Item("landung"))
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("luongdung", row.Item("luongdung"))
                    Me.IGridViewChiTiet02.SetFocusedRowCellValue("dvdung", row.Item("donvidung"))
                    If IsDBNull(Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc")) = False Then
                        Me.GetBangGiaToaThuocTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getBangGiaToaThuoc, CDate(Me.NgaykhamIDateEdit.EditValue))
                        Dim dtRow As DataRow = getRow(Me.PCV_CLINICDataSetTongHop.getBangGiaToaThuoc, Me.GetBangGiaToaThuocBindingSource, "mahang", Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc"))
                        Try
                            Me.IGridViewChiTiet02.SetFocusedRowCellValue("dongia", CDec(dtRow.Item("giaban")))
                        Catch ex As Exception
                            MessageBox.Show("Không tìm được giá - set lại 0")
                            Me.IGridViewChiTiet02.SetFocusedRowCellValue("dongia", 0)
                        End Try
                    End If

                Next row
            End If
            Me.IGridViewPopUp2.SetRowCellValue(index, "Mark", False)
        Next
        'Delete row cuối cùng bị dư
        If IsDBNull(Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc")) Then
            Me.IGridViewChiTiet02.DeleteSelectedRows()
        End If
        'Duyệt xem trong grid có dòng dư
        For index = 0 To IGridViewChiTiet02.RowCount
            If IsDBNull(Me.IGridViewChiTiet02.GetRowCellValue(index, "mathuoc")) Then
                Me.IGridViewChiTiet02.DeleteRow(index)
            End If
        Next

        Me.PopupContainerEdit1.ClosePopup()
    End Sub
    'Kiểm tra sự tồn tại của mathuoc trong IGridViewChitiet02
    Private Function getKiemTraMaTonTaiIGridViewChitiet02(ByVal sMathuoc As String) As Boolean
        Dim acc As Integer
        For index = 0 To Me.IGridViewChiTiet02.RowCount - 2
            If Me.IGridViewChiTiet02.GetRowCellValue(index, "mathuoc").ToString.ToUpper = sMathuoc.ToUpper Then
                acc = acc + 1
            End If
        Next
        If acc > 0 Then
            Return False
        End If
        Return True
    End Function

    Private Sub SimpleButtonLuu_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButtonLuu.Click
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("tenbenhvien", Me.ITextEditChuyenDenBV.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("diachibenhvien", Me.ITextEditDiaChiBV.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("dauhieulamsang", Me.ITextEditDauHieuLS.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("cacxetnghiem", Me.ITextEditCacXetNghiem.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("chandoan", Me.ITextEditChanDoan.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("thuocdadung", Me.ITextEditThuocDaDung.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("tinhtrangnguoibenh", Me.ITextEditTinhTrangBN.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("lydochuyenvien", Me.ITextEditChuyenDenBV.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("phuongtienvanchuyen", Me.ITextEditPhuongTienVC.EditValue)
        Me.IGridViewChiTiet04.SetFocusedRowCellValue("nguoiduadi", Me.ITextEditHTNguoiDua.EditValue)
        Me.PopupContainerEdit2.ClosePopup()
    End Sub

    Private Sub RepositoryItemISearchLookUpEdit_TonKho_LoHandung_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_TonKho_LoHandung.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("mathuoc", editor.Properties.View.GetRowCellValue(rowHandle, "mahang"))
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tenthuoc", editor.Properties.View.GetRowCellValue(rowHandle, "tenhang"))
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "dvt"))
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("dvdung", editor.Properties.View.GetRowCellValue(rowHandle, "dvt"))
        If IsDBNull(Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc")) = False Then
            Me.GetBangGiaToaThuocTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getBangGiaToaThuoc, CDate(Me.NgaykhamIDateEdit.EditValue))
            Dim dtRow As DataRow = getRow(Me.PCV_CLINICDataSetTongHop.getBangGiaToaThuoc, Me.GetBangGiaToaThuocBindingSource, "mahang", Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc"))
            Try
                Me.IGridViewChiTiet02.SetFocusedRowCellValue("dongia", CDec(dtRow.Item("giaban")))
            Catch ex As Exception
                MessageBox.Show("Không tìm được giá" & Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc").ToString & "- set lại 0")
                Me.IGridViewChiTiet02.SetFocusedRowCellValue("dongia", 0)
            End Try
        End If
    End Sub

    Private Sub IGridViewChiTiet02_ShownEditor(sender As System.Object, e As System.EventArgs) Handles IGridViewChiTiet02.ShownEditor
        If Me.IGridViewChiTiet02.FocusedColumn.FieldName = "mathuoc" Then
            Me.GetTonKho_Lo_HanDung_ThucTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTonKho_Lo_HanDung_Thuc, CDate(Today), CDate(Today), " ")
        End If
        If Me.IGridViewChiTiet02.FocusedColumn.FieldName = "soluong" Then
            Me.m_Flag = False
        End If
    End Sub

    Private Sub IGridViewChiTiet02_CellValueChanging(sender As System.Object, e As System.EventArgs) Handles IGridViewChiTiet02.CellValueChanging
        If Me.IGridViewChiTiet02.FocusedColumn.FieldName = "soluong" Then
            Try
                Me.OldValue = CDec(Me.IGridViewChiTiet02.GetFocusedRowCellValue("soluong"))
            Catch ex As Exception
                Me.OldValue = 0
            End Try
        End If
    End Sub

    Private Sub IGridViewChiTiet02_CellValueChanged(sender As System.Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewChiTiet02.CellValueChanged
        Me.TongchiphikhamITextEdit.EditValue = CDec(Me.IGridViewChiTiet01.getSummaryColumn("thanhtien")) + CDec(Me.IGridViewChiTiet02.getSummaryColumn("thanhtien"))
        If Me.IGridViewChiTiet02.FocusedColumn.FieldName = "soluong" AndAlso Not (m_Flag) Then
            Try
                m_Flag = True
                Me.NewValue = CDec(Me.IGridViewChiTiet02.GetFocusedRowCellValue("soluong"))
                Dim mahang As String = Me.IGridViewChiTiet02.GetFocusedRowCellValue("mathuoc")
                MessageBox.Show(mahang)
                Dim soluongton As Decimal = CDec(Me.GetTonKho_Lo_HanDung_ThucTableAdapter.getSoLuongCuoiKy_TonKho_Lo_HanDung_Thuc(CDate(Today), CDate(Today), mahang, " "))
                MessageBox.Show(soluongton)
                If soluongton < Me.NewValue Then
                    If DevExpress.XtraEditors.XtraMessageBox.Show("Số lượng xuất (" & NewValue & ") lớn hơn số lượng tồn (" & soluongton & ") " & vbNewLine & " Bạn vui lòng chọn lại 'Số lượng' ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) = Windows.Forms.DialogResult.Yes Then

                        Me.IGridViewChiTiet02.SetFocusedRowCellValue("soluong", Me.OldValue)

                    End If
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

    Public Overrides Sub setSubSau_Luu()
        Me.NewValue = 0
        Me.OldValue = 0
    End Sub

    Private Sub IGridViewChiTiet01_CellValueChanged(sender As System.Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewChiTiet01.CellValueChanged
        Me.TongchiphikhamITextEdit.EditValue = CDec(Me.IGridViewChiTiet01.getSummaryColumn("thanhtien")) + CDec(Me.IGridViewChiTiet02.getSummaryColumn("thanhtien"))
    End Sub
End Class
